package cn.org.xwb.bookmall.user.controller;

import cn.org.xwb.bookmall.model.constant.AccountConst;
import cn.org.xwb.bookmall.user.service.LoginService;
import cn.org.xwb.common.encrypt.annotation.des.DesEn;
import cn.org.xwb.common.encrypt.annotation.rsa.RsaDe;
import cn.org.xwb.common.encrypt.annotation.rsa.RsaDeParam;
import cn.org.xwb.common.encrypt.constant.EncryptConstant;
import cn.org.xwb.common.error.annotation.ServerErrorCatch;
import cn.org.xwb.common.model.vo.RespResult;
import cn.org.xwb.common.valid.annotation.str.StrNotNull;
import cn.org.xwb.common.valid.annotation.str.StrNotNullParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @GetMapping("/mail/code")
    @ServerErrorCatch
    @StrNotNull
    @RsaDe
    public RespResult<Object> getMailCode(
            @RequestParam("mail") @StrNotNullParam @RsaDeParam
            String mail
    ) {
        return loginService.getMailCode(mail);
    }

    @PostMapping("/mail")
    @ServerErrorCatch
    @StrNotNull
    @RsaDe
    @DesEn
    public RespResult<Object> loginByMail(
            @RequestParam(EncryptConstant.DES_KEY_PARAM_NAME) @StrNotNullParam @RsaDeParam
            String desKey,
            @RequestParam("mail") @StrNotNullParam @RsaDeParam
            String mail,
            @RequestParam("code") @StrNotNullParam @RsaDeParam
            String code
    ) {
        return loginService.loginByMail(mail, code);
    }

    @PostMapping("/pwd")
    @ServerErrorCatch
    @StrNotNull
    @RsaDe
    @DesEn
    public RespResult<Object> loginByPwd(
            @RequestParam(EncryptConstant.DES_KEY_PARAM_NAME) @StrNotNullParam @RsaDeParam
            String desKey,
            @RequestParam("account") @StrNotNullParam @RsaDeParam
            String account,
            @RequestParam("pwd") @StrNotNullParam @RsaDeParam
            String pwd
    ) {
        return loginService.loginByPwd(account, pwd);
    }

    /**
     * 登录是否有效
     *
     * @param account
     * @return
     */
    @GetMapping("/effective")
    @ServerErrorCatch
    @StrNotNull
    @RsaDe
    public RespResult<Object> effetive(
            @RequestParam(AccountConst.ACCOUNT_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String account
    ) {
        return loginService.effective(account);
    }

}
